// main.cpp (测试代码)
#include "SSCADA.h"
#include <cassert>
#include <gflags/gflags.h>

DEFINE_string(csv_filepath, "", "csv file path"); // csv文件路径
DEFINE_string(start_time, "", "开始时间 (格式: HH:MM)");
DEFINE_string(end_time, "", "结束时间 (格式: HH:MM)");

void testSingleton()
{
    SSCADA &instance1 = SSCADA::getInstance();
    SSCADA &instance2 = SSCADA::getInstance();
    assert(&instance1 == &instance2);
}

void testDataLoading()
{
    SSCADA::getInstance().loadMeasurement(FLAGS_csv_filepath);
    // 验证数据加载是否正确
}

void testStatistics()
{
    SSCADA &scada = SSCADA::getInstance();

    if (FLAGS_start_time.empty() && FLAGS_end_time.empty())
    {
        std::cout << "未指定时间段，输出全天测点信息\n";
        scada.printStatInfo();
    }
    else
    {
        struct tm tm = *gmtime(&scada.baseTime);
        time_t start = scada.baseTime;
        time_t end = scada.baseTime + 86100; // 86400秒=24小时

        if (!FLAGS_start_time.empty())
        {
            int start_hour, start_minute;
            sscanf(FLAGS_start_time.c_str(), "%d:%d", &start_hour, &start_minute);//sscanf()函数从一个字符串中读进与指定格式相符的数据
            tm.tm_hour = start_hour;
            tm.tm_min = start_minute;
            tm.tm_sec = 0;
            start = timegm(&tm);
        }

        if (!FLAGS_end_time.empty())
        {
            int end_hour, end_minute;
            sscanf(FLAGS_end_time.c_str(), "%d:%d", &end_hour, &end_minute);
            tm.tm_hour = end_hour;
            tm.tm_min = end_minute;
            tm.tm_sec = 0;
            end = timegm(&tm);
        }

        std::cout << "开始时间: " << FLAGS_start_time << " (" 
                  << std::put_time(gmtime(&start), "%Y-%m-%d %H:%M:%S") << ")\n";
        std::cout << "结束时间: " << FLAGS_end_time << " (" 
                  << std::put_time(gmtime(&end), "%Y-%m-%d %H:%M:%S") << ")\n";

        scada.printStatInfo(start, end);
    }
}

int main(int argc, char *argv[])
{
    gflags::ParseCommandLineFlags(&argc, &argv, true);

    try
    {
        testSingleton();
        testDataLoading();
        testStatistics();
    }
    catch (const std::exception &e)
    {
        std::cerr << "错误: " << e.what() << "\n";
        return 1;
    }
    return 0;
}